﻿/**
* This method handle menu context.
* @param args Unused.
* @return Nothing.
* @exception error when ...
* @see exception
*/
var glbalvar = "This is global!";
$(document).ready(function () {
    window.globalVar = "This is global!";
    $('div' || 'a' || 'h1' || 'h2' | 'h3').mousedown(function (e) {
        if (e.button == 2) {
            $.contextMenu({
                selector: 'div' || 'a' || 'h1' || 'h2' | 'h3',
                items: {
                    key: {
                        name: "Change language key value",
                        type: 'text',
                        value: ""
                    },
                    // <input type="texboxt">
                    LangVI: {
                        name: "Lang vi-VI",
                        type: 'text',
                        value: ""
                    },
                    // <input type="texboxt">
                    LangUS: {
                        name: "Lang en-US",
                        type: 'text',
                        value: ""
                    },
                    // <input type="label">
                    SubmitBtn: {
                        name: "",
                        type: 'button',
                        value: "Save/Lưu",
                        events: {
                            click: function (e) {
                                // add some fancy key handling here
                                updateDataValue();
                              
                            }
                        }

                    }
                },
                events: {
                    show: function (opt) {
                        // this is the trigger element
                        var $this = this;
                        getDataValue();
                        // import states from data store 
                        //$.contextMenu.setInputValues(opt, $this.data());
                        // this basically fills the input commands from an object
                        // like {name: "foo", yesno: true, radio: "3", …}
                    },
                    hide: function (opt) {
                        // this is the trigger element
                        var $this = this;
                        $('input:text[name=context-menu-input-LangUS]').val("");
                        $('input:text[name=context-menu-input-LangVI]').val("");
                        // export states to data store
                        //$.contextMenu.getInputValues(opt, $this.data());
                        // this basically dumps the input commands' values to an object
                        // like {name: "foo", yesno: true, radio: "3", …}
                    }
                }
            });
        }
    });

});
/**
* This method get data from server by ajax jQuery.
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function getDataValue() {
    var slText = '';
    var webroot = $('#hiddenField').attr("value");
    var urlang = webroot + 'library/process_lang.php';
    if (window.getSelection) {
        slText = window.getSelection();
    } else if (document.getSelection) {
        slText = document.getSelection();
    } else if (document.selection) {
        slText = document.selection.createRange().text;
    }
    slText = slText.toString().replace(/ /g, '_');
    var tempText = slText.toLowerCase(); 
    //request ajax to get value from server
    //setTimeout(doSomething, 3000);
    $.ajax({
        url: urlang,
        method: "POST",
        global: false,
        data: {
            ajaxcallid: "26",
            fData: tempText
        },
        dataType: 'html'
        //cache: false
        //timeout: 30000,
        //success: callbackFn
        //global: false
        //async:false
    }).done(function (data) {
        //alert(data);
        $('input:text[name=context-menu-input-LangUS]').val(jQuery.parseJSON(data).en);
        $('input:text[name=context-menu-input-LangVI]').val(jQuery.parseJSON(data).vi);
        $('input:text[name=context-menu-input-key]').val(tempText); 

    }); 

}
/**
* This method get data from server by ajax jQuery.
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function updateDataValue() {
    var slText = '';
    var webroot = $('#hiddenField').attr("value");
    var urlang = webroot + 'library/process_lang.php';
    var path = $('#hiddenFieldPath').val();   
    if (window.getSelection) {
        slText = window.getSelection();
    } else if (document.getSelection) {
        slText = document.getSelection();
    } else if (document.selection) {
        slText = document.selection.createRange().text;
    }    
    // get the form data
    // there are many ways to get this data using jQuery (you can use the class or id also)
    var formData = {
        "key": $('input:text[name=context-menu-input-key]').val(),
        "path": path,
        "val-vi":     $('input:text[name=context-menu-input-LangVI]').val(),
        "val-us":     $('input:text[name=context-menu-input-LangUS]').val()
    };   
    //request ajax to get value from server
    //setTimeout(doSomething, 3000);
    $.ajax({
        url: urlang,
        method: "POST",
        global: false,
        data: {
            ajaxcallid: "28",
            fData: JSON.stringify(formData)
        },
        dataType: 'html'
        //cache: false
        //timeout: 30000,
        //success: callbackFn
        //global: false
        //async:false
    }).done(function (data) {
        $('input:text[name=context-menu-input-key]').val(data);
        alert("Update resource succcessfully!");

    });
    glbalvar = "nothing";
}
/**
* This method ....
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}
/**
* This method ....
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function stateChange(newState) {
    setTimeout(function () {
        if (newState == -1) { alert('Wating for data complete'); }
    }, 800);
}